package com.ruoyi.service.jpjs.dan.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.domain.jpjs.common.dan.Dan;
import com.ruoyi.domain.jpjs.common.dan.Inventory;
import com.ruoyi.domain.jpjs.common.dandetail.DanDetail;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.jpjs.dan.DanMapper;
import com.ruoyi.service.jpjs.dan.DanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author C.fw
 * @explain 混凝土报表业务层处理
 * @since 2020/8/15 15:43
 */
@Service
public class DanServiceImpl implements DanService {

    @Autowired
    DanMapper danMapper;

    @Override
    public List<Inventory> getInventoryList(Map<String, Object> map) {
        return danMapper.getInventoryList(map);
    }

    @Override
    public int deleteInventoryById(String inventory) {
        String[] inventoryId = Convert.toStrArray(inventory);
        return danMapper.deleteInventoryById(inventoryId);
    }

    @Override
    @Transactional
    public int insertDan(Dan dan, MultipartFile[] companyFile, MultipartFile[] expertFile) {
        try {
            danMapper.insertDan(dan);
            int danId = dan.getDanId();

            Inventory inventory = new Inventory();

            // Step1 物理上传
            String companyFileName = "";
            String expertFileName = "";
            if (companyFile.length == 1) {
                companyFileName = FileUploadUtils.upload(Global.getUploadPath(), companyFile[0]);

                inventory.setFileName(companyFileName);
                inventory.setVirtualPath(ServerConfig.getUrl() + companyFileName);
                inventory.setCompanyId(dan.getCompanyId());
                inventory.setProgramId(dan.getProgramId());
                inventory.setType("1");
                inventory.setDanId(String.valueOf(danId));
                danMapper.insertInventory(inventory);
            }
            if (expertFile.length == 1) {
                expertFileName = FileUploadUtils.upload(Global.getUploadPath(), expertFile[0]);

                inventory.setFileName(expertFileName);
                inventory.setVirtualPath(ServerConfig.getUrl() + expertFileName);
                inventory.setCompanyId(dan.getCompanyId());
                inventory.setProgramId(dan.getProgramId());
                inventory.setType("2");
                inventory.setDanId(String.valueOf(danId));
                danMapper.insertInventory(inventory);
            }
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public int updateDanInfo(Dan dan, MultipartFile[] companyFile, MultipartFile[] expertFile) {
        try {
            danMapper.updateDanInfo(dan);

            Inventory inventory = new Inventory();

            // Step1 物理上传
            String companyFileName = "";
            String expertFileName = "";
            if (companyFile.length == 1) {
                companyFileName = FileUploadUtils.upload(Global.getUploadPath(), companyFile[0]);

                inventory.setFileName(companyFileName);
                inventory.setVirtualPath(ServerConfig.getUrl() + companyFileName);
                inventory.setCompanyId(dan.getCompanyId());
                inventory.setProgramId(dan.getProgramId());
                inventory.setType("1");
                inventory.setDanId(String.valueOf(dan.getDanId()));
                danMapper.insertInventory(inventory);
            }
            if (expertFile.length == 1) {
                expertFileName = FileUploadUtils.upload(Global.getUploadPath(), expertFile[0]);

                inventory.setFileName(expertFileName);
                inventory.setVirtualPath(ServerConfig.getUrl() + expertFileName);
                inventory.setCompanyId(dan.getCompanyId());
                inventory.setProgramId(dan.getProgramId());
                inventory.setType("2");
                inventory.setDanId(String.valueOf(dan.getDanId()));
                danMapper.insertInventory(inventory);
            }
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }


    @Override
    public int deleteDanById(String dan) {
        String[] danId = Convert.toStrArray(dan);
        return danMapper.deleteDanById(danId);
    }

    @Override
    public List<Ztree> getProgramTree(String menu) {
        List<Map<String, Object>> list = danMapper.getProgramTree(ShiroUtils.getSysUser().getUserId());
        //危大工程将二公司合并到一公司
        if (menu != null && menu.equals("wdgc")) {
            String oneId = danMapper.getOneCompId();
            String towId = danMapper.getTwoCompId();
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).get("id").toString().equals(towId)){
                    list.remove(i);
                }
                if(list.get(i).get("parentId").toString().equals(towId)){
                    list.get(i).put("parentId",oneId);
                }
            }
        }
        return initZtree(list);
    }

    @Override
    public int updatePathById(Long detailId) {
        return danMapper.updatePathById(detailId);
    }

    @Override
    public int updateDanDetailById(DanDetail danDetail) {
        return danMapper.updateDanDetailById(danDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteFilePathById(Long detailId) {
        Map<String, Object> map = new HashMap<>();
        map.put("danDetailId", detailId);
        List<DanDetail> danDetails = danMapper.selectDanDetailInfo(map);
        return FileUtils.deleteFile(danDetails.get(0).getPhysicalPath());
    }

    @Override
    public int deleteDanDetailById(String danDetail) {
        String[] danDetailId = Convert.toStrArray(danDetail);
        return danMapper.deleteDanDetailById(danDetailId);
    }

    @Override
    public List<DanDetail> selectDanDetailInfo(Map<String, Object> map) {
        return danMapper.selectDanDetailInfo(map);
    }

    @Override
    public List<Dan> getDanList(Map<String, Object> map) {
        return danMapper.getDanList(map);
    }

    @Override
    public int insertDanDetail(DanDetail danDetail) {
        return danMapper.insertDanDetail(danDetail);
    }

    public List<Ztree> initZtree(List<Map<String, Object>> projectList) {
        List<Ztree> ztrees = new ArrayList();
        for (Map<String, Object> map : projectList) {
            Ztree ztree = new Ztree();
            ztree.setId((String) map.get("id"));
            ztree.setpId((String) map.get("parentId"));
            ztree.setName((String) map.get("name"));
            ztree.setTitle((String) map.get("name"));
            ztree.setGrade((String) map.get("grade"));
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public int insertInventory(Inventory inventory) {
        return danMapper.insertInventory(inventory);
    }


}
